using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace Model
{
	public partial class Tbl008_Production : DBInteractionBase
	{
		#region Class Member Declarations
			private SqlBoolean		_pStatus;
			private SqlDateTime		_pCreateDate, _pLastDateModife;
			private SqlInt32		_pAmount, _curID, _curIDOld, _unitID, _unitIDOld;
			private SqlString		_pImage, _pBrief, _pID, _pPrice, _pName, _pColor, _caID, _caIDOld;
		#endregion


		public Tbl008_Production()
		{
			// Nothing for now.
		}


		public override bool Insert()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl008_Production_Insert]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@PID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _pID));
				cmdToExecute.Parameters.Add(new SqlParameter("@UnitID", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _unitID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CurID", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _curID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caID));
				cmdToExecute.Parameters.Add(new SqlParameter("@PName", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pName));
				cmdToExecute.Parameters.Add(new SqlParameter("@PPrice", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pPrice));
				cmdToExecute.Parameters.Add(new SqlParameter("@PCreateDate", SqlDbType.DateTime, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pCreateDate));
				cmdToExecute.Parameters.Add(new SqlParameter("@PLastDateModife", SqlDbType.DateTime, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pLastDateModife));
				cmdToExecute.Parameters.Add(new SqlParameter("@PColor", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pColor));
				cmdToExecute.Parameters.Add(new SqlParameter("@PAmount", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _pAmount));
				int	length = 0;
				if(!_pBrief.IsNull)
				{
					length = _pBrief.Value.Length;
				}
				cmdToExecute.Parameters.Add(new SqlParameter("@PBrief", SqlDbType.NText, length, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pBrief));
				cmdToExecute.Parameters.Add(new SqlParameter("@PImage", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pImage));
				cmdToExecute.Parameters.Add(new SqlParameter("@PStatus", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pStatus));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl008_Production::Insert::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Update()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl008_Production_Update]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@PID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _pID));
				cmdToExecute.Parameters.Add(new SqlParameter("@UnitID", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _unitID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CurID", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _curID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caID));
				cmdToExecute.Parameters.Add(new SqlParameter("@PName", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pName));
				cmdToExecute.Parameters.Add(new SqlParameter("@PPrice", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pPrice));
				cmdToExecute.Parameters.Add(new SqlParameter("@PCreateDate", SqlDbType.DateTime, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pCreateDate));
				cmdToExecute.Parameters.Add(new SqlParameter("@PLastDateModife", SqlDbType.DateTime, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pLastDateModife));
				cmdToExecute.Parameters.Add(new SqlParameter("@PColor", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pColor));
				cmdToExecute.Parameters.Add(new SqlParameter("@PAmount", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _pAmount));
				int	length = 0;
				if(!_pBrief.IsNull)
				{
					length = _pBrief.Value.Length;
				}
				cmdToExecute.Parameters.Add(new SqlParameter("@PBrief", SqlDbType.NText, length, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pBrief));
				cmdToExecute.Parameters.Add(new SqlParameter("@PImage", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pImage));
				cmdToExecute.Parameters.Add(new SqlParameter("@PStatus", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _pStatus));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl008_Production::Update::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public bool UpdateAllWUnitIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl008_Production_UpdateAllWUnitIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@UnitID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _unitID));
				cmdToExecute.Parameters.Add(new SqlParameter("@UnitIDOld", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _unitIDOld));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl008_Production::UpdateAllWUnitIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public bool UpdateAllWCurIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl008_Production_UpdateAllWCurIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CurID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _curID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CurIDOld", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _curIDOld));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl008_Production::UpdateAllWCurIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public bool UpdateAllWCaIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl008_Production_UpdateAllWCaIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _caID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaIDOld", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caIDOld));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl008_Production::UpdateAllWCaIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Delete()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl008_Production_Delete]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@PID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _pID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl008_Production::Delete::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override DataTable SelectOne()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl008_Production_SelectOne]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl008_Production");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@PID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _pID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				if(toReturn.Rows.Count > 0)
				{
					_pID = (string)toReturn.Rows[0]["PID"];
					_unitID = toReturn.Rows[0]["UnitID"] == System.DBNull.Value ? SqlInt32.Null : (Int32)toReturn.Rows[0]["UnitID"];
					_curID = toReturn.Rows[0]["CurID"] == System.DBNull.Value ? SqlInt32.Null : (Int32)toReturn.Rows[0]["CurID"];
					_caID = toReturn.Rows[0]["CaID"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["CaID"];
					_pName = toReturn.Rows[0]["PName"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["PName"];
					_pPrice = toReturn.Rows[0]["PPrice"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["PPrice"];
					_pCreateDate = toReturn.Rows[0]["PCreateDate"] == System.DBNull.Value ? SqlDateTime.Null : (DateTime)toReturn.Rows[0]["PCreateDate"];
					_pLastDateModife = toReturn.Rows[0]["PLastDateModife"] == System.DBNull.Value ? SqlDateTime.Null : (DateTime)toReturn.Rows[0]["PLastDateModife"];
					_pColor = toReturn.Rows[0]["PColor"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["PColor"];
					_pAmount = toReturn.Rows[0]["PAmount"] == System.DBNull.Value ? SqlInt32.Null : (Int32)toReturn.Rows[0]["PAmount"];
					_pBrief = toReturn.Rows[0]["PBrief"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["PBrief"];
					_pImage = toReturn.Rows[0]["PImage"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["PImage"];
					_pStatus = toReturn.Rows[0]["PStatus"] == System.DBNull.Value ? SqlBoolean.Null : (bool)toReturn.Rows[0]["PStatus"];
				}
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl008_Production::SelectOne::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public override DataTable SelectAll()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl008_Production_SelectAll]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl008_Production");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl008_Production::SelectAll::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public DataTable SelectAllWUnitIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl008_Production_SelectAllWUnitIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl008_Production");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@UnitID", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _unitID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl008_Production::SelectAllWUnitIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public DataTable SelectAllWCurIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl008_Production_SelectAllWCurIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl008_Production");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CurID", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _curID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl008_Production::SelectAllWCurIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public DataTable SelectAllWCaIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl008_Production_SelectAllWCaIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl008_Production");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl008_Production::SelectAllWCaIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		#region Class Property Declarations
		public SqlString PID
		{
			get
			{
				return _pID;
			}
			set
			{
				SqlString pIDTmp = (SqlString)value;
				if(pIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("PID", "PID can't be NULL");
				}
				_pID = value;
			}
		}


		public SqlInt32 UnitID
		{
			get
			{
				return _unitID;
			}
			set
			{
				_unitID = value;
			}
		}
		public SqlInt32 UnitIDOld
		{
			get
			{
				return _unitIDOld;
			}
			set
			{
				_unitIDOld = value;
			}
		}


		public SqlInt32 CurID
		{
			get
			{
				return _curID;
			}
			set
			{
				_curID = value;
			}
		}
		public SqlInt32 CurIDOld
		{
			get
			{
				return _curIDOld;
			}
			set
			{
				_curIDOld = value;
			}
		}


		public SqlString CaID
		{
			get
			{
				return _caID;
			}
			set
			{
				_caID = value;
			}
		}
		public SqlString CaIDOld
		{
			get
			{
				return _caIDOld;
			}
			set
			{
				_caIDOld = value;
			}
		}


		public SqlString PName
		{
			get
			{
				return _pName;
			}
			set
			{
				_pName = value;
			}
		}


		public SqlString PPrice
		{
			get
			{
				return _pPrice;
			}
			set
			{
				_pPrice = value;
			}
		}


		public SqlDateTime PCreateDate
		{
			get
			{
				return _pCreateDate;
			}
			set
			{
				_pCreateDate = value;
			}
		}


		public SqlDateTime PLastDateModife
		{
			get
			{
				return _pLastDateModife;
			}
			set
			{
				_pLastDateModife = value;
			}
		}


		public SqlString PColor
		{
			get
			{
				return _pColor;
			}
			set
			{
				_pColor = value;
			}
		}


		public SqlInt32 PAmount
		{
			get
			{
				return _pAmount;
			}
			set
			{
				_pAmount = value;
			}
		}


		public SqlString PBrief
		{
			get
			{
				return _pBrief;
			}
			set
			{
				_pBrief = value;
			}
		}


		public SqlString PImage
		{
			get
			{
				return _pImage;
			}
			set
			{
				_pImage = value;
			}
		}


		public SqlBoolean PStatus
		{
			get
			{
				return _pStatus;
			}
			set
			{
				_pStatus = value;
			}
		}
		#endregion
	}
}
